/*
 UICard01
 UILZBJPKRank 操作按钮层
 */
var app = require("lzbjpk_app");
cc.Class({
    extends: require(app.subGameName + "_BaseForm"),

    properties: {
        // check_Special: cc.Button,
        cardPrefab: cc.Prefab,
        // icon_mapai: cc.SpriteFrame,
    },

    OnCreateInit: function () {
        //节点
        this.card = this.node.getChildByName('card');
        this.btnDun1 = this.GetWndNode("nodeDun/btnDun1");
        this.btnDun2 = this.GetWndNode("nodeDun/btnDun2");
        this.btnDun3 = this.GetWndNode("nodeDun/btnDun3");
        this.btnDun4 = this.GetWndNode("nodeDun/btnDun4");
        this.btnDun5 = this.GetWndNode("nodeDun/btnDun5");
        this.btnDun6 = this.GetWndNode("nodeDun/btnDun6");
        this.btnDun7 = this.GetWndNode("nodeDun/btnDun7");
        this.btnDun8 = this.GetWndNode("nodeDun/btnDun8");
        this.btnDun9 = this.GetWndNode("nodeDun/btnDun9");

        this.check_duizi = this.GetWndNode("node_btn_select/check_duizi");
        this.check_shunzi = this.GetWndNode("node_btn_select/check_shunzi");
        this.check_tonghua = this.GetWndNode("node_btn_select/check_tonghua");
        this.check_tonghuashun = this.GetWndNode("node_btn_select/check_tonghuashun");
        this.check_santiao= this.GetWndNode("node_btn_select/check_santiao");

        this.no_duizi = this.GetWndNode('node_btn_unSelect/no_duizi');
        this.no_shunzi = this.GetWndNode('node_btn_unSelect/no_shunzi');
        this.no_tonghua = this.GetWndNode('node_btn_unSelect/no_tonghua');
        this.no_tonghuashun = this.GetWndNode('node_btn_unSelect/no_tonghuashun');
        this.no_santiao = this.GetWndNode('node_btn_unSelect/no_santiao');

        // this.check_Special = 

        this.btnCloseDun1 = this.GetWndNode('nodeDun/btnCloseDun1').getComponent(cc.Button);
        this.btnCloseDun2 = this.GetWndNode('nodeDun/btnCloseDun2').getComponent(cc.Button);
        this.btnCloseDun3 = this.GetWndNode('nodeDun/btnCloseDun3').getComponent(cc.Button);

        this.btnOK = this.node.getChildByName("btnOK");
        this.btnCancel = this.node.getChildByName("btnCancel");

        this.node_btn_select = this.node.getChildByName("node_btn_select");
        this.node_btn_unSelect =this.node.getChildByName("node_btn_unSelect");

        this.lbl_count = this.GetWndNode("sp_clock/lbl_count");

        this.lb_cardType1 = this.node.getChildByName("lb_cardType1");
        this.lb_cardType2 = this.node.getChildByName("lb_cardType2");
        this.lb_cardType3 = this.node.getChildByName("lb_cardType3");

        //管理器
        this.BJPKRoomMgr = app[app.subGameName.toUpperCase() + "RoomMgr"]();
        this.BJPKRoomPosMgr = app[app.subGameName.toUpperCase() + "RoomPosMgr"]();
        this.ShareDefine = app[app.subGameName + "_ShareDefine"]();
        this.SysDataManager = app[app.subGameName + "_SysDataManager"]();
        this.LogicBJPKRank = app[app.subGameName.toUpperCase() + "LogicRank"]();
        this.LogicBJPKGame = app[app.subGameName.toUpperCase() + "LogicGame"]();
        this.BJPKDefine = app[app.subGameName.toUpperCase() + "Define"]();
        this.PokerCard = app[app.subGameName.toUpperCase() + "PokerCard"]();

        this.trusteeshipTime = {1: 90000, 2: 180000, 3: 60000};
        this.sortType = 2;

        this.RegEvent("EVT_DUN_UPDATE", this.ShowAllPlayerCard);
        this.RegEvent('CardRepeat', this.Event_CardRepeat, this);//缓存了上一把的牌
        this.RegEvent(app.subGameName.toUpperCase() +'_Ranked', this.Event_Ranked, this);//缓存了上一把的牌

        cc.game.on(cc.game.EVENT_SHOW, this.OnEventShow.bind(this));

        this.isBtnHide(false);
        this.card.removeAllChildren();

        for (let idx = 1; idx <= 9; idx++) {
            //9个蹲位的牌创建出来
            let path = "nodeDun/btnDun" + idx.toString();
            let node = this.GetWndNode(path);
            let card = cc.instantiate(this.cardPrefab);
            node.addChild(card);
            //手牌位置创建9张牌
            let btnNode = cc.instantiate(this.cardPrefab);
            btnNode.name = "btn_" + idx;
            btnNode.active = false;
            this.card.addChild(btnNode);
        }

        this.specialType = {
            84: "三顺子",
            85: "三同花",
            86: "六对半",
            87: "五对三条",
            88: "四套三条",
            89: "中原一点色",
            90: "凑一色",
            91: "全小",
            92: "六六大顺",
            93: "全大",
            94: "三分天下",
            95: "三同花顺",
            96: "十二皇族",
            97: "一条龙",
            98: "七星连珠",
            99: "八仙过海",
            100: "至尊清龙"
        };

        // this.wanfa = -1;// 0为可以弃牌，1为不能弃牌
    },


    //--------------显示函数-----------------
    OnShow: function () {
        let room = this.BJPKRoomMgr.GetEnterRoom();
        this.xianShi = room.GetRoomConfigByProperty("xianShi");
        this.setStartTime = room.GetRoomSet().GetRoomSetProperty("setStartTime");
        this.setCurrentTime = room.GetRoomSet().GetRoomSetProperty("setCurrentTime");
        let totalTime = parseInt(parseInt(this.setCurrentTime - this.setStartTime) / 1000);
        this.countDown = this.trusteeshipTime[this.xianShi] / 1000;
        this.countDown -= totalTime;
        this.FormManager.CloseForm("UIGameHelp");
        this.FormManager.CloseForm(app.subGameName + "_UISetting02");
        this.FormManager.CloseForm("UIBJPK_CardType");
        this.FormManager.CloseForm("UIRecord");
        this.FormManager.CloseForm("UIUserInfo");
        this.FormManager.CloseForm("UIChat");

        this.SetStartInit();
        this.HideOrShowTiShi(false);
        this.lbl_count.getComponent(cc.Label).string = "";
        if (this.xianShi > 0) {
            this.lbl_count.getComponent(cc.Label).string = (this.countDown).toString();
            this.schedule(this.CallEverySecond, 1);
        }

        //发牌效果
        this.DealCardEffect();

        this.lb_cardType1.active = false;
        this.lb_cardType2.active = false;
        this.lb_cardType3.active = false;
        let wanfa = room.GetRoomConfigByProperty("wanfa");
        if (wanfa == 0) {
            this.node.getChildByName("btnQipai").active = true;
        } else if (wanfa == 1) {
            this.node.getChildByName("btnQipai").active = false;
        }
    },

    Event_CardRepeat: function (event) {
        this.ReInRoom();
    },
    Event_Ranked: function (event) {
        let dunPos = event["dunPos"];
        let isChange = event["isChange"];
        let first = dunPos["first"];
        let second = dunPos["second"];
        let third = dunPos["third"];
        let dunListDict = {
            1: first,
            2: second,
            3: third
        };
        this.LogicBJPKRank.cardStateList['DUN1'] = first;
        this.LogicBJPKRank.cardStateList['DUN2'] = second;
        this.LogicBJPKRank.cardStateList['DUN3'] = third;
        this.ShowAllDUN();
        this.HideOrShowTiShi(isChange);
    },
    ReInRoom: function () {
        let roomID = this.BJPKRoomMgr.GetEnterRoomID();
        this.BJPKRoomMgr.SendGetRoomInfo(roomID);
    },
    InitCardPos: function () {
        this.ChooseCardAddOffY = 45;
        this.InitBtnCardPosY = -30;
        this.cardSpcedX = 134;//卡牌间的距离
        this.fristCardX = 0;
        this.startIndex = -1;
        this.endIndex = -1;
        // let touchSprite = this.GetWndNode("touchSprite");
        this.card.on(cc.Node.EventType.TOUCH_START, this.OnTouchStart, this);
        this.card.on(cc.Node.EventType.TOUCH_MOVE, this.OnTouchMove, this);
        this.card.on(cc.Node.EventType.TOUCH_END, this.OnTouchEnd, this);
        this.card.on(cc.Node.EventType.TOUCH_CANCEL, this.OnTouchCancel, this);
    },

    //展示提示状态
    HideOrShowTiShi: function (isShow) {
        this.node.getChildByName("img_tishi").active = isShow;
    },

    OnTouchStart: function (event) {
        /*let card = this.card;
         let moveX = event.target.convertToNodeSpaceAR(event.touch.getLocation()).x;
         //let downList = this.LogicBJPKRank.getDunListByType("DOWN");
         for(let i = 0;i < card.children.length; i++){
         if(card.children[i].name.startsWith("btn")){
         let minX = card.children[i].x - card.children[i].width/2;
         let maxX = minX + this.cardSpcedX;
         if(moveX >= minX && moveX < maxX){
         this.startIndex = i;
         card.children[i].getChildByName("cardPrefab").getChildByName("poker_back").active = true;
         break;
         }
         }
         }*/

        this.startIndex = -1;
        let moveX = event.target.convertToNodeSpaceAR(event.touch.getLocation()).x;
        let downList = this.LogicBJPKRank.getDunListByType("DOWN");
        for (let i = 0; i < downList.length; i++) {
            let cardNode = this.card.children[i];
            if (cardNode.name.startsWith("btn") && cardNode.active) {
                let minX = cardNode.x - cardNode.width / 2;
                let maxX = minX + this.cardSpcedX;
                if (i == downList.length - 1) {
                    maxX = minX + cardNode.width;
                }
                if (moveX >= minX && moveX < maxX) {
                    let color = cc.color(180, 180, 180);
                    cardNode.children[0].color = color;
                    this.startIndex = i;
                    return;
                }
            }
        }
    },

    OnTouchMove: function (event) {
        /*let card = this.card;
         let moveX = event.target.convertToNodeSpaceAR(event.touch.getLocation()).x;
         //let downList = this.LogicBJPKRank.getDunListByType("DOWN");
         for(let i = 0; i < card.children.length; i++){
         if(card.children[i].name.startsWith("btn")){
         let minX = card.children[i].x - card.children[i].width/2;
         let maxX = minX + this.cardSpcedX;
         if(moveX >= minX && moveX < maxX){
         this.moveIndex = i;
         break;
         }
         }
         }
         if(this.startIndex > this.moveIndex){//从右往左
         for(let i = this.startIndex; i >= this.moveIndex; i--){
         if(card.children[i].name.startsWith("btn")){
         card.children[i].getChildByName("cardPrefab").getChildByName("poker_back").active = true;
         }
         }
         }
         else{//从左往右
         for(let i = this.startIndex; i <= this.moveIndex; i++){
         if(card.children[i].name.startsWith("btn")){
         card.children[i].getChildByName("cardPrefab").getChildByName("poker_back").active = true;
         }
         }
         }*/

        this.moveIndex = this.startIndex;
        let moveX = event.target.convertToNodeSpaceAR(event.touch.getLocation()).x;
        let downList = this.LogicBJPKRank.getDunListByType("DOWN");
        for (let i = 0; i < downList.length; i++) {
            let cardNode = this.card.children[i];
            if (cardNode.name.startsWith("btn") && cardNode.active) {
                let minX = cardNode.x - cardNode.width / 2;
                let maxX = minX + this.cardSpcedX;
                if (moveX >= minX && moveX < maxX) {
                    this.moveIndex = i;
                    break;
                }
            }
        }
        if (this.startIndex > -1) {//选牌中没有理牌中的牌
            if (this.startIndex > this.moveIndex) {//从右往左
                for (let i = this.startIndex; i >= this.moveIndex; i--) {
                    let cardNode = this.card.children[i];
                    if (cardNode.name.startsWith("btn")) {
                        let color = cc.color(180, 180, 180);
                        cardNode.children[0].color = color;
                    }
                }
            } else {//从左往右
                for (let i = this.startIndex; i <= this.moveIndex; i++) {
                    let cardNode = this.card.children[i];
                    if (cardNode.name.startsWith("btn")) {
                        let color = cc.color(180, 180, 180);
                        cardNode.children[0].color = color;
                    }
                }
            }
        }
    },

    OnTouchEnd: function (event) {
        /*let card = this.card;
         let moveX = event.target.convertToNodeSpaceAR(event.touch.getLocation()).x;
         //let downList = this.LogicBJPKRank.getDunListByType("DOWN");
         for(let i = 0;i<card.children.length;i++){
         if(card.children[i].name.startsWith("btn")){
         card.children[i].getChildByName("cardPrefab").getChildByName("poker_back").active = false;
         if(isEnd) continue;
         let isEnd = false;
         let minX = card.children[i].x - card.children[i].width/2;
         let maxX = minX + this.cardSpcedX;
         if(moveX >= minX && moveX < maxX){
         this.endIndex = i;
         isEnd = true;
         //break;
         }
         }
         }

         for(let j = 0;j<card.children.length;j++){
         if(this.startIndex > this.endIndex){//右往左
         if(j <= this.startIndex && j >= this.endIndex && card.children[j].children[0].active)
         this.Click_card(card.children[j].name,card.children[j]);
         }
         else if(this.startIndex < this.endIndex){//左往右
         if(j >= this.startIndex && j <= this.endIndex && card.children[j].children[0].active)
         this.Click_card(card.children[j].name,card.children[j]);
         }
         else{
         if(j == this.endIndex && card.children[j].children[0].active){
         this.Click_card(card.children[j].name,card.children[j]);
         break;
         }
         }
         }*/

        //如果点击的牌中有理好的牌型就全部竖起来
        this.endIndex = this.moveIndex;
        let moveX = event.target.convertToNodeSpaceAR(event.touch.getLocation()).x;
        let downList = this.LogicBJPKRank.getDunListByType("DOWN");
        for (let i = 0; i < downList.length; i++) {
            let cardNode = this.card.children[i];
            if (cardNode.name.startsWith("btn") && cardNode.active) {

                if (isEnd) continue;
                let isEnd = false;
                let minX = cardNode.x - cardNode.width / 2;
                let maxX = minX + this.cardSpcedX;
                if (i == downList.length - 1) {
                    maxX = minX + cardNode.width;
                }
                if (moveX >= minX && moveX < maxX) {
                    this.endIndex = i;
                    isEnd = true;
                }
            }
        }

        if (this.startIndex > -1) {//选牌中没有理牌中的牌
            if (this.startIndex == this.endIndex) {//点击牌浮起
                if (this.card.children[this.endIndex].active && !this.card.children[this.endIndex].touchTag) {
                    this.Click_card(this.card.children[this.endIndex].name, this.card.children[this.endIndex]);
                }
            } else {
                if (this.startIndex > this.endIndex) {//从右往左 大于起点
                    for (let i = this.startIndex; i >= this.endIndex; i--) {
                        if (this.card.children[i].active) {
                            this.Click_card(this.card.children[i].name, this.card.children[i]);
                        }
                    }
                } else {//从左往右 小于起点
                    for (let i = this.startIndex; i <= this.endIndex; i++) {
                        if (this.card.children[i].active) {
                            this.Click_card(this.card.children[i].name, this.card.children[i]);
                        }
                    }
                }
            }
        }
    },

    OnTouchCancel: function (event) {
        /*let card = this.card;
         for(let i = 0;i<card.children.length;i++){
         if(card.children[i].name.startsWith("btn")){
         card.children[i].getChildByName("cardPrefab").getChildByName("poker_back").active = false;
         }
         }*/
        //同OnTouchEnd
        this.endIndex = this.moveIndex;
        let moveX = event.target.convertToNodeSpaceAR(event.touch.getLocation()).x;
        let downList = this.LogicBJPKRank.getDunListByType("DOWN");
        for (let i = 0; i < downList.length; i++) {
            let cardNode = this.card.children[i];
            if (cardNode.name.startsWith("btn") && cardNode.active) {
                if (isEnd) continue;
                let isEnd = false;
                let minX = cardNode.x - cardNode.width / 2;
                let maxX = minX + this.cardSpcedX;
                if (i == downList.length - 1) {
                    maxX = minX + cardNode.width;
                }

                if (moveX >= minX && moveX < maxX) {
                    this.endIndex = i;
                    isEnd = true;
                } else if (moveX < minX) {//moveX<minX  moveX>maxX 往左移超出了左框
                    this.endIndex = 0;
                    isEnd = true;
                } else {//往右滑动超出了右框
                    this.endIndex = downList.length - 1;
                    isEnd = true;
                }
            }
        }

        if (this.startIndex > -1) {
            if (this.startIndex == this.endIndex) {//点击牌浮起
                if (this.card.children[this.endIndex].active && !this.card.children[this.endIndex].touchTag) {
                    this.Click_card(this.card.children[this.endIndex].name, this.card.children[this.endIndex]);
                }
                this.card.children[this.endIndex].touchTag = false;
            } else {
                if (this.startIndex > this.endIndex) {//从右往左 大于起点
                    for (let i = this.startIndex; i >= this.endIndex; i--) {
                        if (this.card.children[i].active) {
                            this.Click_card(this.card.children[i].name, this.card.children[i]);
                        }
                        this.card.children[i].touchTag = false;
                    }
                } else {//从左往右 小于起点
                    for (let i = this.startIndex; i <= this.endIndex; i++) {
                        if (this.card.children[i].active) {
                            this.Click_card(this.card.children[i].name, this.card.children[i]);
                        }
                        this.card.children[i].touchTag = false;
                    }
                }
            }
        }
    },

    OnReload: function () {
        console.log("OnReload");
    },

    //隐藏发送和取消按钮
    isBtnHide: function (show) {
        this.btnOK.active = show;
        this.btnCancel.active = show;
        if (show) {
            this.Click_SendRankList(false);
        }
    },

    //回合开始的初始化
    SetStartInit: function () {
        this.card.active = 1;
        this.LogicBJPKRank.InitDunState();
        if (this.LogicBJPKRank.GetIsSixteen()) {
            this.card.children[this.card.children.length - 1].active = true;
            this.card.children[this.card.children.length - 2].active = true;
            this.card.children[this.card.children.length - 3].active = true;
            this.card.getComponent(cc.Layout).spacingX = -15;
            this.cardCount = 9;
        }
        else {
            this.card.children[this.card.children.length - 1].active = false;
            this.card.children[this.card.children.length - 2].active = false;
            this.card.children[this.card.children.length - 3].active = false;
            this.card.getComponent(cc.Layout).spacingX = 5;
            this.cardCount = 9;
        }
        // this.NoticeSpecial();
        this.InitCardPos();
        this.ClearNodeSprite();
        this.disabledBtn();
    },

    OnSetEnd: function () {
        let room = this.BJPKRoomMgr.GetEnterRoom();
        if (!room) {
            console.error("OnSetEnd not enter room");
            return
        }
    },

    NoticeSpecial: function () {
        /*if(this.LogicBJPKRank.GetSpecialType() == -1){
         this.check_Special.interactable = false;
         }
         else{
         this.check_Special.interactable = true;
         let type = this.specialType[(this.LogicBJPKRank.GetSpecialType()).toString()];
         this.WaitForConfirm("MSG_SPACIAL_CARD", [type], [], this.ShareDefine.ConfirmYN);
         }*/
    },

    ClearNodeSprite: function () {
        // 手牌
        for (let card_index = 1; card_index <= this.cardCount; card_index++) {
            let btnNode = this.GetWndNode("card/btn_" + card_index);
            if (!btnNode) {
                console.error("ClearNodeSprite not find:%s", btnNode);
                continue;
            }
            btnNode.active = 0;
            btnNode.y = this.InitBtnCardPosY;
        }

        //墩位牌
        for (let idx = 1; idx <= 9; idx++) {
            let btnNode = this.GetWndNode("nodeDun/btnDun" + idx);
            if (!btnNode) {
                console.error("ClearNodeSprite not find:%s", "btnDun" + idx);
                continue;
            }
            let cardNode = btnNode.getChildByName("cardPrefab");
            cardNode.active = 0;
        }
    },


    OnEventShow: function () {
        if (this.xianShi == 0) {
            this.lbl_count.getComponent(cc.Label).string = "";
            return;
        }
        let self = this;
        app[app.subGameName + "_NetManager"]().SendPack("base.CServerTime", {}, function (event) {
            self.setCurrentTime = event.serverTimeMsec;
            let totalTime = parseInt(parseInt(self.setCurrentTime - self.setStartTime) / 1000);
            self.countDown = self.trusteeshipTime[self.xianShi] / 1000;
            self.countDown -= totalTime;
        });
        /*let totalTime = parseInt(parseInt(this.setCurrentTime - this.setStartTime) / 1000);
         this.countDown = this.trusteeshipTime[this.xianShi] / 1000;
         this.countDown -= totalTime;*/

        if (this.countDown <= 0) {
            this.lbl_count.getComponent(cc.Label).string = "0";
        }
    },

    CallEverySecond: function () {
        this.countDown--;
        this.lbl_count.getComponent(cc.Label).string = (this.countDown).toString();
        if (this.countDown == this.BJPKDefine.CountDownNotice) {
            /*if (app[app.subGameName + "_ShareDefine"]().isCoinRoom) {
             app[app.subGameName + "_SysNotifyManager"]().ShowSysMsg('MSG_COUNT_NOTICE');
             }*/
        } else if (this.countDown <= 0) {
            this.unschedule(this.CallEverySecond);
            this.lbl_count.getComponent(cc.Label).string = "0";
            /*if (app[app.subGameName + "_ShareDefine"]().isCoinRoom) {
             this.CloseForm();
             app[app.subGameName + "_GameManager"]().SendAutoStart();
             }*/
        }
    },

    ShowAllPlayerCard: function () {
        this.lb_cardType1.active = false;
        this.lb_cardType2.active = false;
        this.lb_cardType3.active = false;
        this.ClearNodeSprite();
        console.log('=======ShowAllPlayerCard1=========',this.LogicBJPKRank.getDunListByType("DUN" + 1));
     
        //处理手牌
        let downList = this.LogicBJPKRank.getDunListByType("DOWN");
        for (let index = 0; index < downList.length; index++) {
            let cardType = downList[index];
            let btnNode = this.GetWndNode("card/btn_" + (index + 1));
            if (!btnNode) {
                console.error("ShowAllPlayerCard not find:%s", btnNode);
                continue;
            }
            btnNode.children[0].color = cc.color(255, 255, 255);
            btnNode.active = true;
            //位置
            btnNode.y = this.InitBtnCardPosY;
            let bSelected = this.LogicBJPKRank.CheckSelected(cardType);
            if (bSelected) btnNode.y += this.ChooseCardAddOffY;

            // this.ShowCard(cardType, btnNode.getChildByName("cardPrefab"));
            this.ShowCard(cardType, btnNode);
        }

        console.log('=======ShowAllPlayerCard2=========',this.LogicBJPKRank.getDunListByType("DUN" + 1));

        //处理墩位牌
        for (let dunType = 1; dunType <= 3; dunType++) {
            let dunList = this.LogicBJPKRank.getDunListByType("DUN" + dunType);
            //这里对数据进行了排序，有癞子了就需要注意，没有癞子的时候是使用这个方法，有癞子了就需要注意
            console.log("=========UILZBJPKRank ShowAllPlayerCard 排序前============",dunList);
            this.LogicBJPKGame.SortCardByMin(dunList);
            console.log("=========UILZBJPKRank ShowAllPlayerCard 排序后============",dunList);
            //23A 566
            if (this.LogicBJPKGame.GetCardValue(dunList[0]) == 2 &&
                this.LogicBJPKGame.GetCardValue(dunList[1]) == 3 &&
                this.LogicBJPKGame.GetCardValue(dunList[2]) == 14) {
                dunList.unshift(dunList[2]);
                dunList.pop();
            } else if (this.LogicBJPKGame.GetCardValue(dunList[0]) < this.LogicBJPKGame.GetCardValue(dunList[1]) &&
                this.LogicBJPKGame.GetCardValue(dunList[1]) == this.LogicBJPKGame.GetCardValue(dunList[2])) {
                dunList.reverse();
            }
            let dunStartIdx = 0;
            this.GetCardType('DUN' + dunType);
            if (2 == dunType) dunStartIdx = 3;
            if (3 == dunType) dunStartIdx = 6;
            for (let idx = 0; idx < dunList.length; idx++) {
                let dunIdx = dunStartIdx + idx + 1;
                let btnNode = this.GetWndNode("nodeDun/btnDun" + dunIdx);
                if (!btnNode) {
                    console.error("ShowAllPlayerCard not find:%s", "btnDun" + dunIdx);
                    continue
                }
                this.ShowCard(dunList[idx], btnNode.getChildByName("cardPrefab"));
            }
        }
    },

    ShowAllDUN: function () {
        //墩位
        for (let dunType = 1; dunType <= 3; dunType++) {
            let dunList = this.LogicBJPKRank.getDunListByType("DUN" + dunType);
            this.LogicBJPKGame.SortCardByMin(dunList);
            //23A
            if (this.LogicBJPKGame.GetCardValue(dunList[0]) == 2 &&
                this.LogicBJPKGame.GetCardValue(dunList[1]) == 3 &&
                this.LogicBJPKGame.GetCardValue(dunList[2]) == 14) {
                dunList.unshift(dunList[2]);
                dunList.pop();
            } else if (this.LogicBJPKGame.GetCardValue(dunList[0]) < this.LogicBJPKGame.GetCardValue(dunList[1]) &&
                this.LogicBJPKGame.GetCardValue(dunList[1]) == this.LogicBJPKGame.GetCardValue(dunList[2])) {
                dunList.push(dunList[0]);
                dunList.shift();
            }
            let dunStartIdx = 0;
            this.GetCardType('DUN' + dunType);
            if (2 == dunType) dunStartIdx = 3;
            if (3 == dunType) dunStartIdx = 6;
            for (let idx = 0; idx < dunList.length; idx++) {
                let dunIdx = dunStartIdx + idx + 1;
                let btnNode = this.GetWndNode("nodeDun/btnDun" + dunIdx);
                if (!btnNode) {
                    console.error("ShowAllPlayerCard not find:%s", "btnDun" + dunIdx);
                    continue;
                }
                this.ShowCard(dunList[idx], btnNode.getChildByName("cardPrefab"));
            }
        }
    },

    //发牌效果
    DealCardEffect: function () {
        let self = this;
        let downList = this.LogicBJPKRank.getDunListByType("DOWN");
        console.log('===展示发牌效果 downList====', JSON.stringify(downList));
        for (let cardIdx = 1; cardIdx <= this.cardCount; cardIdx++) {
            let btnNode = this.GetWndNode("card/btn_" + cardIdx);
            if (!btnNode) {
                console.error("DealCardEffect not find:%s", btnNode);
                continue;
            }
            btnNode.active = 1;
            // this.ShowCard(downList[cardIdx - 1], btnNode.getChildByName("cardPrefab"));
            this.ShowCard(downList[cardIdx - 1], btnNode);
            btnNode.active = true;
        }
    },

    ShowCard: function (cardType, btnNode) {
        let newPoker = this.PokerCard.SubCardValue(cardType); //逻辑牌值
        this.PokerCard.GetPokeCard(newPoker, btnNode);

        btnNode.getChildByName("poker_back").active = false;
        btnNode.active = true;

        /*let room = this.BJPKRoomMgr.GetEnterRoom();
         if(!room)  return;

         let child = btnNode.getChildByName("icon_mapai");
         if(child){
         child.removeFromParent();
         }

         let maPaiValue = room.GetRoomProperty("mapai");
         if(newPoker == maPaiValue){
         let node = new cc.Node();
         node.name = "icon_mapai";
         let sp = node.addComponent(cc.Sprite);
         sp.spriteFrame = this.icon_mapai;
         btnNode.addChild(node);
         }*/
    },
    disabledBtn: function () {
        let shouCardList = this.LogicBJPKRank.getDunListByType("DOWN");
        console.log("==========Rank==disabledBtn=========",JSON.stringify(shouCardList));
        if (!shouCardList.length) {
            this.isBtnHide(true);
            this.node_btn_select.active = false;
            this.node_btn_unSelect.active = false;
        } else if (this.LogicBJPKRank.GetIsSixteen() && shouCardList.length <= 3) { //是否是13张
            this.isBtnHide(true);
            this.node_btn_select.active = false;
            this.node_btn_unSelect.active = false;
        } else {
            this.isBtnHide(false);
            this.node_btn_select.active = true;
            this.node_btn_unSelect.active = true;
            console.log("==========Rank==disabledBtn1=========",JSON.stringify(this.LogicBJPKRank.getDunListByType("DOWN")));
            let duizis = app[app.subGameName.toUpperCase() + "LogicGame"]().GetDuiZi(shouCardList);
            console.log("==========Rank==disabledBtn2=========",JSON.stringify(this.LogicBJPKRank.getDunListByType("DOWN")));
            let shunzi = app[app.subGameName.toUpperCase() + "LogicGame"]().GetShunzi(shouCardList);
            console.log("==========Rank==disabledBtn3=========",JSON.stringify(this.LogicBJPKRank.getDunListByType("DOWN")));
            let tonghua = app[app.subGameName.toUpperCase() + "LogicGame"]().GetTonghua(shouCardList);
            console.log("==========Rank==disabledBtn4=========",JSON.stringify(this.LogicBJPKRank.getDunListByType("DOWN")));
            let tonghuashun = app[app.subGameName.toUpperCase() + "LogicGame"]().GetTongHuaShun(shouCardList);
            console.log("==========Rank==disabledBtn5=========",JSON.stringify(this.LogicBJPKRank.getDunListByType("DOWN")));
            let santiaos = app[app.subGameName.toUpperCase() + "LogicGame"]().GetSanTiao(shouCardList);
            // let liangdui = app[app.subGameName.toUpperCase()+"LogicGame"]().GetLiangDui(shouCardList);
            // let zhadan = app[app.subGameName.toUpperCase()+"LogicGame"]().GetZhaDang(shouCardList);
            // let wutong = app[app.subGameName.toUpperCase()+"LogicGame"]().GetWuTong(shouCardList);

            console.log("==========Rank==disabledBtn6=========",JSON.stringify(this.LogicBJPKRank.getDunListByType("DOWN")));
            if (!duizis.length) {
                this.check_duizi.active = false;
                this.no_duizi.active = true;
            } else {
                this.check_duizi.active = true;
                this.no_duizi.active = false;
            }
            /*if(!liangdui.length){
             this.check_liangdui.active = false;
             this.no_liangdui.active = true;
             }else{
             if(liangdui.length == 2){
             let bRet = this.LogicBJPKGame.CheckSameValue(liangdui[0], liangdui[1]);
             if(bRet){
             this.check_liangdui.active = false;
             this.no_liangdui.active = true;
             }
             else{
             this.check_liangdui.active = true;
             this.no_liangdui.active = false;
             }
             }
             else{
             this.check_liangdui.active = true;
             this.no_liangdui.active = false;
             }
             }*/
            if (!santiaos.length) {
                this.check_santiao.active = false;
                this.no_santiao.active = true;
            } else {
                this.check_santiao.active = true;
                this.no_santiao.active = false;
            }
            if (!shunzi.length) {
                this.check_shunzi.active = false;
                this.no_shunzi.active = true;
            } else {
                this.check_shunzi.active = true;
                this.no_shunzi.active = false;
            }
            if (!tonghua.length) {
                this.check_tonghua.active = false;
                this.no_tonghua.active = true;
            } else {
                this.check_tonghua.active = true;
                this.no_tonghua.active = false;
            }
            /*if(duizis.length != 0 && santiaos.length != 0){

             let bhulu = false;
             for(let idx = 0;idx < duizis.length; idx++){
             let duizi = duizis[idx];
             for(let i = 0;i < santiaos.length; i++){
             let santiao = santiaos[i];
             let bRet = this.LogicBJPKGame.CheckSameValue(santiao, duizi);
             if(!bRet){
             bhulu = true;
             break;
             }
             }
             if(bhulu){
             break;
             }
             }

             if(bhulu){
             this.check_hulu.active = true;
             this.no_hulu.active = false;
             }
             else{
             this.check_hulu.active = false;
             this.no_hulu.active = true;
             }

             }else{
             this.check_hulu.active = false;
             this.no_hulu.active = true;
             }*/
            /*if(!zhadan.length){
             this.check_zhadang.active = false;
             this.no_zhadang.active = true;
             }else{
             this.check_zhadang.active = true;
             this.no_zhadang.active = false;
             }*/
            if (!tonghuashun.length) {
                this.check_tonghuashun.active = false;
                this.no_tonghuashun.active = true;
            } else {
                this.check_tonghuashun.active = true;
                this.no_tonghuashun.active = false;
            }
            /*if(!wutong.length){
             this.check_wutong.active = false;
             this.no_wutong.active = true;
             } else{
             this.check_wutong.active = true;
             this.no_wutong.active = false;
             }*/
        }
    },

    // 刷新手牌
    reSortCards: function () {
        let pokers = this.LogicBJPKRank.getDunListByType('DOWN');
        let sortCards = this.LogicBJPKGame.GetSortCards(pokers);
        if (sortCards && sortCards.length) {
            this.LogicBJPKRank.pushDownCards(sortCards);
        }
    },
    reSortCardsByColor: function () {
        let pokers = this.LogicBJPKRank.getDunListByType('DOWN');
        if (this.sortType == 1) {
            this.sortType = 2
        } else {
            this.sortType = 1;
        }
        let sortCards = this.LogicBJPKGame.GetSortCardsByColor(pokers, this.sortType);
        if (sortCards && sortCards.length) {
            this.LogicBJPKRank.pushDownCards(sortCards);
        }
    },
    reSortCardsByMax: function () {
        let pokers = this.LogicBJPKRank.getDunListByType('DOWN');
        if (this.sortType == 1) {
            this.sortType = 2
        } else {
            this.sortType = 1;
        }
        let sortCards = this.LogicBJPKGame.GetSortCardsByMax(pokers, this.sortType);
        if (sortCards && sortCards.length) {
            this.LogicBJPKRank.pushDownCards(sortCards);
        }
    },
    GetCardType: function (dun) {

        let len = this.LogicBJPKRank.GetDunLength(dun);
        let typNode = {};
        let bShow = false;
        if (dun == 'DUN1' && len == 3) {
            // typNode	= this.GetWndNode("bg_dun/lb_cardType1");
            // typNode.active = true;
            bShow = true;
        } else if (dun == 'DUN2' && len == 5) {
            // typNode	= this.GetWndNode("bg_dun/lb_cardType2");
            // typNode.active = true;
            bShow = true;
        } else if (dun == 'DUN3' && len == 5) {
            // typNode	= this.GetWndNode("bg_dun/lb_cardType3");
            // typNode.active = true;
            bShow = true;
        }

        if (bShow) {
            /*let dunList = this.LogicBJPKRank.getDunListByType(dun);
             let type = this.LogicBJPKGame.CheckCardType(dunList);
             if(type == 0){
             typNode.getComponent(cc.Label).string = "对子";
             }
             else if(type == 2){
             typNode.getComponent(cc.Label).string = "三条";
             }
             else if(type == 3){
             typNode.getComponent(cc.Label).string = "顺子";
             }
             else if(type == 4){
             typNode.getComponent(cc.Label).string = "同花";
             }
             else if(type == 5){
             typNode.getComponent(cc.Label).string = "一对同花";
             }
             else if(type == 6){
             typNode.getComponent(cc.Label).string = "两对同花";
             }
             else if(type == 9){
             typNode.getComponent(cc.Label).string = "同花顺";
             }*/
        }
    },

    //---------点击触发---------------------
    OnClick: function (btnName, btnNode) {
        console.log('btnName', btnName);
        if (btnName.startsWith("btn_")) {
            this.Click_card(btnName, btnNode);
        }
        else if (btnName == "btnDun1") {
            this.LogicBJPKRank.ClearOneCard('DUN1', 0);
            this.LogicBJPKRank.AutoSetDun();
            this.reSortCards();
            this.disabledBtn();
        }
        else if (btnName == "btnDun2") {
            this.LogicBJPKRank.ClearOneCard('DUN1', 1);
            this.LogicBJPKRank.AutoSetDun();
            this.reSortCards();
            this.disabledBtn();
        }
        else if (btnName == "btnDun3") {
            this.LogicBJPKRank.ClearOneCard('DUN1', 2);
            this.LogicBJPKRank.AutoSetDun();
            this.reSortCards();
            this.disabledBtn();
        }
        else if (btnName == "btnDun4") {
            this.LogicBJPKRank.ClearOneCard('DUN2', 0);
            this.LogicBJPKRank.AutoSetDun();
            this.reSortCards();
            this.disabledBtn();
        }
        else if (btnName == "btnDun5") {
            this.LogicBJPKRank.ClearOneCard('DUN2', 1);
            this.LogicBJPKRank.AutoSetDun();
            this.reSortCards();
            this.disabledBtn();
        }
        else if (btnName == "btnDun6") {
            this.LogicBJPKRank.ClearOneCard('DUN2', 2);
            this.LogicBJPKRank.AutoSetDun();
            this.reSortCards();
            this.disabledBtn();
        }
        else if (btnName == "btnDun7") {
            this.LogicBJPKRank.ClearOneCard('DUN3', 0);
            this.LogicBJPKRank.AutoSetDun();
            this.reSortCards();
            this.disabledBtn();
        }
        else if (btnName == "btnDun8") {
            this.LogicBJPKRank.ClearOneCard('DUN3', 1);
            this.LogicBJPKRank.AutoSetDun();
            this.reSortCards();
            this.disabledBtn();
        }
        else if (btnName == "btnDun9") {
            this.LogicBJPKRank.ClearOneCard('DUN3', 2);
            this.LogicBJPKRank.AutoSetDun();
            this.reSortCards();
            this.disabledBtn();
        }
        else if (btnName == "check_duizi") { //点击对子
            this.LogicBJPKRank.CheckDuiZi();
        }
        /*else if (btnName == "check_liangdui"){
         this.LogicBJPKRank.CheckLiangDui();
         }*/
        else if (btnName == "check_santiao") {
            this.LogicBJPKRank.CheckSanTiao();
        }
        else if (btnName == "check_shunzi") {
            this.LogicBJPKRank.CheckShunzi();
        }
        else if (btnName == "check_tonghua") {
            this.LogicBJPKRank.CheckTonghua();
        }
        /*else if (btnName == "check_hulu"){
         this.LogicBJPKRank.CheckHulu();
         }
         else if (btnName.startsWith("check_zhadang")){
         this.LogicBJPKRank.CheckZhaDang();
         }*/
        else if (btnName == "check_tonghuashun") {
            this.LogicBJPKRank.CheckTongHuaShun();
        }
        /*else if(btnName	== "check_wutong"){
         this.LogicBJPKRank.CheckWuTong();
         }*/
        else if (btnName == "check_special") {
            this.LogicBJPKRank.SetSpecialCard();
            this.LogicBJPKRank.SendRankList();
            this.CloseForm();
        }
        else if (btnName == "btnCloseDun1") {
            if (this.LogicBJPKRank.getDunListByType('DUN1').length) {
                this.LogicBJPKRank.ClearDun('DUN1', true);
                this.reSortCards();
                this.disabledBtn();
            }
        }
        else if (btnName == "btnCloseDun2") {
            if (this.LogicBJPKRank.getDunListByType('DUN2').length) {
                this.LogicBJPKRank.ClearDun('DUN2', true);
                this.reSortCards();
                this.disabledBtn();
            }
        }
        else if (btnName == "btnCloseDun3") {
            if (this.LogicBJPKRank.getDunListByType('DUN3').length) {
                this.LogicBJPKRank.ClearDun('DUN3', true);
                this.reSortCards();
                this.disabledBtn();
            }
        }
        else if (btnName == "btnCancel") {
            this.HideOrShowTiShi(false);
            this.LogicBJPKRank.ClearDun('DUN1', false);
            this.LogicBJPKRank.ClearDun('DUN2', false);
            this.LogicBJPKRank.ClearDun('DUN3', true);
            this.lb_cardType1.active = false;
            this.lb_cardType2.active = false;
            this.lb_cardType3.active = false;
            this.reSortCards();
            this.disabledBtn();
        }
        else if (btnName == "btnCardtype") {
            this.FormManager.ShowOrCloseForm("UIBJPK_CardType");
        }
        else if (btnName == "BtnDownCards") {
            this.LogicBJPKRank.clearSelectedCards();
        }
        else if (btnName == "btnFirst") {
            this.LogicBJPKRank.SetDun('DUN1');
            this.disabledBtn();
        }
        else if (btnName == "btnSecond") {
            this.LogicBJPKRank.SetDun('DUN2');
            this.disabledBtn();
        }
        else if (btnName == "btnThird") {
            this.LogicBJPKRank.SetDun('DUN3');
            this.disabledBtn();
        }
        else if (btnName == "btnQipai") {
            console.log("点击弃牌按钮!");
            this.Click_SendQiPai();
        }
        else if (btnName == "btnOK") { //点击比牌
            this.Click_SendRankList(true);
        } else if (btnName == "btnAhs") {
            console.log("点击按花色按钮!");
            this.reSortCardsByColor();
        } else if (btnName == "btnAdx") {
            console.log("点击按大小按钮!");
            this.reSortCardsByMax();
        }
        else {
            console.error("btnName:%s not find", btnName);
        }
    },

    Click_card: function (btnName, clickNode) {
        console.log(btnName);
        // clickNode.getChildByName("cardPrefab").getChildByName("poker_back").active = false;
        clickNode.getChildByName("poker_back").active = false;
        let reg = new RegExp("btn_", "g");
        if (btnName.indexOf("btn_") != -1) {
            var result = btnName.replace(reg, "");
            btnName = result;
        }
        let cardIdx = btnName.toString();

        if (clickNode.y == this.InitBtnCardPosY) {
            this.LogicBJPKRank.SetCardSelected(cardIdx);
        } else {
            this.LogicBJPKRank.DeleteCardSelected(cardIdx);
            clickNode.children[0].color = cc.color(255, 255, 255);
        }
    },
    Click_SendQiPai: function () {
        let msgID = "playQiPai";
        app[app.subGameName + "_ConfirmManager"]().SetWaitForConfirmForm(this.OnConFirm.bind(this), msgID, []);
        app[app.subGameName + "_ConfirmManager"]().ShowConfirm(this.ShareDefine.Confirm, msgID, []);
    },

    //点击比牌
    Click_SendRankList: function (isBtnType) {
        let bAllRanked = this.LogicBJPKRank.CheckAllRanked();
        if (bAllRanked) {
            /*this.LogicBJPKRank.SendRankList();
             let isSuccess = this.LogicBJPKRank.CheckSendRankList();
             if (isSuccess) {
             // this.CloseForm();
             } else {
             this.ShowAllDUN();
             }*/
            let room = this.BJPKRoomMgr.GetEnterRoom();
            let roomID = room.GetRoomProperty("roomID");
            let pid = app[app.subGameName + "_HeroManager"]().GetHeroProperty("pid");
            let posID = this.BJPKRoomPosMgr.GetClientPos();

            let sendPack = {
                roomID: roomID, //房间id
                pid: pid,    //玩家id
                posID: posID,  //玩家位置
                // isSpecial:this.isSpecial,
                btnType: isBtnType,// isBtnType == false不要发 iscardready
                dunPos: {
                    first: this.LogicBJPKRank.cardStateList["DUN1"],
                    second: this.LogicBJPKRank.cardStateList["DUN2"],
                    third: this.LogicBJPKRank.cardStateList["DUN3"],
                }
            };
            console.log("发送比牌data", sendPack);
            let msg = `${app.subGameName}.C${app.subGameName.toUpperCase()}Ranked`;
            app[app.subGameName + "_NetManager"]().SendPack(msg, sendPack, (data) => {
                if (data.isChange) {
                    this.LogicBJPKRank.cardStateList['DUN1'] = data["dunPos"]["first"];
                    this.LogicBJPKRank.cardStateList['DUN2'] = data["dunPos"]["second"];
                    this.LogicBJPKRank.cardStateList['DUN3'] = data["dunPos"]["third"];
                    console.log("服务端传回正确的牌型", data["dunPos"]);
                    this.ShowAllDUN();
                    this.HideOrShowTiShi(data.isChange);
                }
            });
        }
    },

    Click_Dun: function (btnName, clickNode) {
        this.LogicBJPKRank.SetDun(btnName.toUpperCase());
    },

    //确认框点击
    OnConFirm: function (clickType, msgID, backArgList) {
        if (clickType != "Sure") {
            return
        } else if (msgID == "playQiPai") {
            this.LogicBJPKRank.SendPosQiPai();
        }
        if (msgID == "MSG_SPACIAL_CARD") {
            this.LogicBJPKRank.SetSpecialCard();
            this.LogicBJPKRank.SendRankList();
            this.CloseForm();
        }
    },
});
